﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Slaughter;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Slaughter
{
    /// <summary>
    /// 根据生产批次获取屠宰信息请求命令处理类
    /// </summary>
    public class GetSlaughterHandler:IRequestHandler<GetSlaughterCommand,ApiResult<List<SlaughterDTO>>>
    {
        IBaseRepository<Slaughter_Information> slaughterRep;
        IMapper mapper;

        public GetSlaughterHandler(IBaseRepository<Slaughter_Information> slaughterRep, IMapper mapper)
        {
            this.slaughterRep = slaughterRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<SlaughterDTO>>> Handle(GetSlaughterCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<SlaughterDTO>> res = new ApiResult<List<SlaughterDTO>>();
                var slaughter = slaughterRep.GetAll().Where(x => x.Production_Batch == request.production_Batch && !x.IsDeleted);
                var model = mapper.Map<List<SlaughterDTO>>(slaughter);
                res.Msg = "根据生产批次获取屠宰信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
